package jinyilw.swing.visuals.Icomp;

import jinyilw.common.Operator;
import jinyilw.swing.i18n.ICSetting;

import javax.swing.Icon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class IMenuLabel extends ILabel
{
	private static final long serialVersionUID = 8480216856075497089L;
	private volatile ArrayList<Operator<MouseEvent>> operators;

	public IMenuLabel()
	{
	}

	public IMenuLabel(String text)
	{
		super(text);
	}

	public IMenuLabel(Icon image)
	{
		super(image);
	}

	public IMenuLabel(String text, int horizontalAlignment)
	{
		super(text, horizontalAlignment);
	}

	public IMenuLabel(Icon image, int horizontalAlignment)
	{
		super(image, horizontalAlignment);
	}

	public IMenuLabel(String text, Icon icon, int horizontalAlignment)
	{
		super(text, icon, horizontalAlignment);
	}

	public IMenuLabel(ICSetting iSetting)
	{
		super(iSetting);
	}

	public IMenuLabel(ICSetting iSetting, int horizontalAlignment)
	{
		super(iSetting, horizontalAlignment);
	}

	public IMenuLabel(ICSetting iSetting, Icon icon, int horizontalAlignment)
	{
		super(iSetting, icon, horizontalAlignment);
	}

	private synchronized void createOperators()
	{
		if (operators == null)
		{
			operators = new ArrayList<>();
			addMouseListener(new MouseAdapter()
			{
				@Override
				public void mouseClicked(MouseEvent e)
				{
					if (isEnabled())
						for (Operator<MouseEvent> operator : operators)
							operator.operate(null);
				}
			});
		}
	}

	public void addClickOperator(Operator<MouseEvent> operator)
	{
		if (operators == null)
			createOperators();
		operators.add(operator);
	}
}
